import wx


class MyFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title="Calculator", pos=wx.DefaultPosition,
                          size=wx.Size(350, 250), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)

        bSizer21 = wx.BoxSizer(wx.VERTICAL)

        bSizer21.Add((0, 0), 1, wx.EXPAND, 5)

        self.m_textCtrl1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.Size(330, -1), 0)
        bSizer21.Add(self.m_textCtrl1, 0, wx.ALL, 5)

        bSizer100 = wx.BoxSizer(wx.VERTICAL)

        bSizer101 = wx.BoxSizer(wx.HORIZONTAL)

        self.m_button1 = wx.Button(self, wx.ID_ANY, u"1", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer101.Add(self.m_button1, 0, wx.ALL, 5)
        self.m_button1.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button2 = wx.Button(self, wx.ID_ANY, u"2", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer101.Add(self.m_button2, 0, wx.ALL, 5)
        self.m_button2.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button3 = wx.Button(self, wx.ID_ANY, u"3", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer101.Add(self.m_button3, 0, wx.ALL, 5)
        self.m_button3.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button4 = wx.Button(self, wx.ID_ANY, u"+", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer101.Add(self.m_button4, 0, wx.ALL, 5)
        self.m_button4.Bind(wx.EVT_BUTTON, self.on_operator_click)

        bSizer100.Add(bSizer101, 1, wx.EXPAND, 5)

        bSizer102 = wx.BoxSizer(wx.HORIZONTAL)

        self.m_button5 = wx.Button(self, wx.ID_ANY, u"4", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer102.Add(self.m_button5, 0, wx.ALL, 5)
        self.m_button5.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button6 = wx.Button(self, wx.ID_ANY, u"5", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer102.Add(self.m_button6, 0, wx.ALL, 5)
        self.m_button6.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button7 = wx.Button(self, wx.ID_ANY, u"6", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer102.Add(self.m_button7, 0, wx.ALL, 5)
        self.m_button7.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button8 = wx.Button(self, wx.ID_ANY, u"-", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer102.Add(self.m_button8, 0, wx.ALL, 5)
        self.m_button8.Bind(wx.EVT_BUTTON, self.on_operator_click)

        bSizer100.Add(bSizer102, 1, wx.EXPAND, 5)

        bSizer103 = wx.BoxSizer(wx.HORIZONTAL)

        self.m_button9 = wx.Button(self, wx.ID_ANY, u"7", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer103.Add(self.m_button9, 0, wx.ALL, 5)
        self.m_button9.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button10 = wx.Button(self, wx.ID_ANY, u"8", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer103.Add(self.m_button10, 0, wx.ALL, 5)
        self.m_button10.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button11 = wx.Button(self, wx.ID_ANY, u"9", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer103.Add(self.m_button11, 0, wx.ALL, 5)
        self.m_button11.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button12 = wx.Button(self, wx.ID_ANY, u"*", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer103.Add(self.m_button12, 0, wx.ALL, 5)
        self.m_button12.Bind(wx.EVT_BUTTON, self.on_operator_click)

        bSizer100.Add(bSizer103, 1, wx.EXPAND, 5)

        bSizer104 = wx.BoxSizer(wx.HORIZONTAL)

        self.m_button13 = wx.Button(self, wx.ID_ANY, u"0", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer104.Add(self.m_button13, 0, wx.ALL, 5)
        self.m_button13.Bind(wx.EVT_BUTTON, self.on_number_click)

        self.m_button14 = wx.Button(self, wx.ID_ANY, u"/", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer104.Add(self.m_button14, 0, wx.ALL, 5)
        self.m_button14.Bind(wx.EVT_BUTTON, self.on_operator_click)

        self.m_button15 = wx.Button(self, wx.ID_ANY, u"AC", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer104.Add(self.m_button15, 0, wx.ALL, 5)
        self.m_button15.Bind(wx.EVT_BUTTON, self.on_clear_click)

        self.m_button16 = wx.Button(self, wx.ID_ANY, u"=", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer104.Add(self.m_button16, 0, wx.ALL, 5)
        self.m_button16.Bind(wx.EVT_BUTTON, self.on_equal_click)

        bSizer100.Add(bSizer104, 1, wx.EXPAND, 5)

        bSizer21.Add(bSizer100, 1, wx.EXPAND, 5)

        bSizer21.Add((0, 0), 1, wx.EXPAND, 5)

        self.SetSizer(bSizer21)
        self.Layout()

        self.Centre(wx.BOTH)

        self.expression = ""

    def on_number_click(self, event):
        button = event.GetEventObject()
        self.expression += button.GetLabel()
        self.m_textCtrl1.SetValue(self.expression)

    def on_operator_click(self, event):
        button = event.GetEventObject()
        if self.expression and self.expression[-1] not in "+-*/":
            self.expression += button.GetLabel()
            self.m_textCtrl1.SetValue(self.expression)

    def on_clear_click(self, event):
        self.expression = ""
        self.m_textCtrl1.SetValue("")

    def on_equal_click(self, event):
        try:
            result = eval(self.expression)
            self.expression = str(result)
            self.m_textCtrl1.SetValue(self.expression)
        except Exception as e:
            self.m_textCtrl1.SetValue("Error")


app = wx.App(False)
frame = MyFrame(None)
frame.Show(True)
app.MainLoop()
